<template>
  <div class="container">
    <div class="app-container">
      <el-row type="flex" justify="start" style="padding: 20px;">
        <el-button size="mini" type="primary" @click="addPermission(0,1)">添加权限</el-button>
      </el-row>
      <el-table
        row-key="id"
        default-expand-all
        :data="permissionList"
        highlight-current-row
        border
        stripe
      >
        <el-table-column prop="name" label="名称" header-class-name="highlight-header" />
        <el-table-column prop="code" label="标识" header-class-name="highlight-header" />
        <el-table-column prop="description" label="描述" header-class-name="highlight-header" />
        <el-table-column label="操作" header-class-name="highlight-header">
          <template v-slot="{ row }">
            <el-button v-if="row.type===1" type="text" size-mini @click="addPermission(row.id,2)">添加</el-button>
            <el-button type="text" size-mini @click="getPermissionDetail(row.id)">编辑</el-button>
            <el-button type="text" size-mini @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增 弹层 -->
    <el-dialog :title="`${showText}权限点`" width="500px" :visible.sync="showDialog" @close="btnCancel">
      <el-form ref="form" :model="formDate" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formDate.name" size="mini" placeholder="请输入权限名称" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formDate.code" size="mini" placeholder="请输入权限标识" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formDate.description" placeholder="在这里输入权限描述" size="60px" />
        </el-form-item>
        <!-- 开启选项 -->
        <el-form-item label="启用">
          <el-switch v-model="formDate.enVisible" active-value="1" inactive-value="0" size="mini" />
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="center">
        <el-button style="margin-right: 20px;" type="primary" size="mini" @click="btnOk">确定</el-button>
        <el-button size="mini" @click="btnCancel">取消</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, addPermission, deletePermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { listToTreeDate } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      showDialog: false,
      permissionList: [],
      formDate: {
        name: '',
        code: '',
        description: '',
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      },

      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限标识', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 通过计算属性获取权限列表
    showText() {
      return this.formDate.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const result = await getPermissionList()
      this.permissionList = listToTreeDate(result, 0)
    },
    headerClassName({ columnIndex }) {
      return 'highlight-header'
    },
    // 添加权限
    addPermission(pid, type) {
      this.formDate.pid = pid
      this.formDate.type = type
      this.formDate.id = null
      this.formDate.enVisible = '0'
      this.showDialog = true
    },
    // 确定
    btnOk() {
      // 校验表单
      this.$refs.form.validate(async isOkey => {
        if (isOkey) {
          // 校验完毕判断是添加还是编辑
          if (this.formDate.id) {
            // 编辑 传入当前编辑的权限的id 更新
            await updatePermission(this.formDate)
            // 提示消息
            this.$message.success('更新成功')
            // 重新获取
            this.getPermissionList()
          } else {
            // 添加
            await addPermission(this.formDate)
            // 提示消息
            this.$message.success(`添加${this.formDate.type === 1 ? '根' : this.formDate.type === 2 ? '按钮' : '无'}权限点成功`)
            // 重新获取
            this.getPermissionList()
          }
          this.btnCancel()
        }
      })
    },
    // 取消
    btnCancel() {
      this.$refs.form.resetFields()
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      }

      this.showDialog = false
    },
    // 删除
    delPermission(id) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 删除权限
        await deletePermission(id)
        // 提示消息
        this.$message.success('删除成功')
        // 重新获取权限列表
        this.getPermissionList()
      })
    },
    // 编辑回显数据
    async getPermissionDetail(id) {
      this.formDate = await getPermissionDetail(id)
      this.showDialog = true
    }
  }
}
</script>
<style scoped>
.highlight-header {
  background-color: pink !important; /* You can choose any color you like */
  font-weight: bold !important;
  color: skyblue !important; /* Optional: change the text color */
}
</style>
